package leeon.forpeddy.htmlparse;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leeon.util.HtmlUtil;

public class SubjectHtmlParser {
	
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	
	public SubjectHtmlParser(String file) throws IOException {
		//读取
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		br.close();
		String html = sb.toString();
		
		String table = HtmlUtil.findTag(html, "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#90C900\" width=\"100%\" id=\"AutoNumber1\" bgcolor=\"#FEF2D8\">", "</table>");
		String[] trs = HtmlUtil.findTable(table);
		for (int i = 1; i < trs.length ; i ++) {
			String[] tds = HtmlUtil.findTrLower(trs[i], "td");
			Map<String, String> row = new HashMap<String, String>();
			row.put("code", tds[1]);
			row.put("name", tds[2]);
			row.put("year", tds[4]);
			list.add(row);
		}
	}
	
	public List<Map<String, String>> getResults(String subject, String area) {
		for (Map<String, String> row : list) {
			row.put("subject", subject);
			row.put("area", area);
		}
		return list;
	}

	public static void main(String[] args) throws IOException {
		SubjectHtmlParser chp = new SubjectHtmlParser("D:\\Program Files\\europa\\jee\\workspace\\forpeddy\\misc\\18_工商管理(物流与报关).html");
		System.out.println(chp.getResults("45", "上中"));
	}

}
